package vues;

import java.awt.EventQueue;
import javax.swing.ImageIcon;
import javax.swing.JFrame;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.border.EmptyBorder;
import javax.swing.JButton;
import javax.swing.JLabel;
import java.awt.Font;
import javax.swing.JTextField;
import javax.swing.JPasswordField;

import modeles.Parametres;

import java.awt.Cursor;
import java.awt.Toolkit;

import java.awt.event.ActionListener;
import java.awt.event.ActionEvent;
import java.awt.event.KeyAdapter;
import java.awt.event.KeyEvent;

public class Securite extends JFrame {
	private static final long serialVersionUID = 1L;
	
	private JPanel contentPane;
	private JLabel lblCadre;
	private JLabel lblConnexion;
	private JLabel lblUsager;
	private JTextField txtUsager;
	private JLabel lblMotDePasse;
	private JPasswordField pswMotDePasse;
	private JButton btnValider;
	private JButton btnQuitter;

	public Securite() {
		setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
		setBounds(100, 100, 450, 311);
		setResizable(false);
		contentPane = new JPanel();
		contentPane.setBorder(new EmptyBorder(5, 5, 5, 5));
		setContentPane(contentPane);
		contentPane.setLayout(null);
		contentPane.add(getLblConnexion());
		contentPane.add(getLblUsager());
		contentPane.add(getTxtUsager());
		contentPane.add(getLblMotDePasse());
		contentPane.add(getPswMotDePasse());
		contentPane.add(getBtnValider());
		contentPane.add(getBtnQuitter());
		contentPane.add(getLblCadre());
		
		setTitle("Gestion des albums");
		setLocationRelativeTo(null);
		getRootPane().setDefaultButton(btnValider);
		setIconImage(Toolkit.getDefaultToolkit().getImage(getClass().getResource("/images/connexion/logoEclipse.png")));
	}
	
	private JLabel getLblCadre() {
		if (lblCadre == null) {
			lblCadre = new JLabel("");
			lblCadre.setBounds(12, 13, 414, 203);
			lblCadre.setIcon(new ImageIcon(Securite.class.getResource("/images/connexion/ban.png")));
		}
		return lblCadre;
	}
	private JLabel getLblConnexion() {
		if (lblConnexion == null) {
			lblConnexion = new JLabel(" Connexion au programme");
			lblConnexion.setFont(new Font("Segoe UI", Font.PLAIN, 24));
			lblConnexion.setBounds(5, 10, 400, 74);
			lblConnexion.setIcon(new ImageIcon(Securite.class.getResource("/images/connexion/ecran.png")));
		}
		return lblConnexion;
	}
	private JLabel getLblUsager() {
		if (lblUsager == null) {
			lblUsager = new JLabel();
			lblUsager.setText("Nom d'utilisateur");
			lblUsager.setFont(new Font("Segoe UI", Font.PLAIN, 14));
			lblUsager.setBounds(43, 114, 129, 20);
			lblUsager.setIcon(new ImageIcon(Securite.class.getResource("/images/connexion/utilisateur.png")));
		}
		return lblUsager;
	}
	private JTextField getTxtUsager() {
		if (txtUsager == null) {
			txtUsager = new JTextField();
			txtUsager.setFont(new Font("Segoe UI", Font.PLAIN, 14));
			txtUsager.setBounds(181, 113, 224, 28);
		}
		return txtUsager;
	}
	private JLabel getLblMotDePasse() {
		if (lblMotDePasse == null) {
			lblMotDePasse = new JLabel();
			lblMotDePasse.setText("Mot de passe");
			lblMotDePasse.setFont(new Font("Segoe UI", Font.PLAIN, 14));
			lblMotDePasse.setBounds(43, 157, 129, 20);
			lblMotDePasse.setIcon(new ImageIcon(Securite.class.getResource("/images/connexion/key.png")));
		}
		return lblMotDePasse;
	}
	private JPasswordField getPswMotDePasse() {
		if (pswMotDePasse == null) {
			pswMotDePasse = new JPasswordField();
			pswMotDePasse.setFont(new Font("Tahoma", Font.PLAIN, 14));
			pswMotDePasse.setBounds(181, 154, 225, 28);
		}
		return pswMotDePasse;
	}
	private JButton getBtnValider() {
		if (btnValider == null) {
			btnValider = new JButton();
			btnValider.setText("Valider");
			btnValider.setFont(new Font("Segoe UI", Font.PLAIN, 12));
			btnValider.setBounds(212, 216, 98, 28);
			btnValider.setIcon(new ImageIcon(Securite.class.getResource("/images/connexion/valider.png")));
			btnValider.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					if(Parametres.validerUsager(txtUsager.getText(), pswMotDePasse.getPassword())){
						MenuApp menu = new MenuApp();
						menu.setVisible(true);
						dispose();
					}else{
						JOptionPane.showMessageDialog(null, "Le nom d'usager ou le mot de passe est invalide", "Mauvaise information", JOptionPane.ERROR_MESSAGE);
					}
				}
			});
			btnValider.setCursor(new Cursor(Cursor.HAND_CURSOR));
		}
		return btnValider;
	}	
	private JButton getBtnQuitter() {
		if (btnQuitter == null) {
			btnQuitter = new JButton();
			btnQuitter.setText("Quitter");
			btnQuitter.setFont(new Font("Segoe UI", Font.PLAIN, 12));
			btnQuitter.setBounds(309, 216, 98, 28);
			btnQuitter.setIcon(new ImageIcon(Securite.class.getResource("/images/connexion/cancel.png")));
			btnQuitter.addActionListener(new ActionListener() {
				public void actionPerformed(ActionEvent e) {
					dispose();
				}				
			});
			btnQuitter.addKeyListener(new KeyAdapter() {   
				public void keyPressed(KeyEvent e) {   
				    if(e.getKeyCode() == 10){
				    	dispose();
				    } 
				}
		    });
			btnQuitter.setCursor(new Cursor(Cursor.HAND_CURSOR));
		}
		return btnQuitter;
	}
	
	
	
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Securite frame = new Securite();				
					frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}
}
